<template>
  <view>
      <view class="goods-list">
        <navigator v-for="(item,index) in goodsList" :key="index" @click="gotuDetail(item)" url="">
           <my-goods :item="item"></my-goods>
        </navigator>
      </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 请求数据的参数
        querObj: {
          // 查询关键词
          query: '',
          // 商品分类Id
          cid: '',
          // 页码值
          pagenum: 1,
          // 每页显示多少条数据
          pagesize: 10
        },
        // 获取商品列表数据
        goodsList: [],
        total: 0,
        // 节流阀
       isloding:false
      };
    },
    onLoad(options) {
      // this.querObj.cid = options.cid || ''
      this.querObj.query = options.query || ''
      console.log(this.querObj);
      // 获取商品列表数据
      this.getGoodsList()
    },
    methods: {
      async getGoodsList(cd) {
        // 打开节流阀
        this.isloding=true
        const {data:res} = await uni.$http.get('/api/public/v1/goods/search',this.querObj)
        // 关闭节流阀
        this.isloding=false
        cd&&cd()
        if (res.meta.status !== 200) {
          return uni.showToast({
            title: '数据请求失败',
            duration: 1500,
            icon: "none"
          })
        }
        console.log(res);
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total=res.message.total
      },
      gotuDetail(item){
        uni.navigateTo({
          url:"/subpkg/goods_detail/goods_detail?goods_id="+item.goods_id
        })
      }
    },
    onReachBottom() {
      if (this.querObj.pagenum * this.querObj.pagesize >= this.total) return uni.showToast({
        title:"我也是有尽头的",
        icon:'none'
      })
      if(this.isloding==true){
        return
      }else{
        // 让页面值加一
        this.querObj.pagenum++
        this.getGoodsList()
      }
    },
    onPullDownRefresh() {
      // 重置关键数据
      this.querObj.pagenum=1
      this.total=0
      this.isloding=false
      this.goodsList=[]
      // 重新发送请求
      this.getGoodsList(
      // 关闭下拉刷新页面
      ()=>uni.stopPullDownRefresh()
      )
    }
  }
</script>

<style lang="scss">

</style>
